package org.geeks.demo;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.Thumbnails.Builder;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * Combine watermark for full image
 * 
 * @author geek-s
 *
 */
public class WaterMarkShowcase2 {

	public static void main(String[] args) throws Exception {
		long begin = System.currentTimeMillis();
		Builder<File> builder = Thumbnails.of("bg.png").scale(1);
		Integer width = builder.asBufferedImage().getWidth();

		Integer height = builder.asBufferedImage().getHeight();

		BufferedImage watermark = ImageIO.read(new File("watermark.png"));

		builder.watermark(Positions.TOP_LEFT, watermark, 1F);

		Integer widthTemp = 0;

		while (width > (widthTemp = widthTemp + watermark.getWidth())) {
			builder.watermark(getPoint(widthTemp, 0), watermark, 1F);
		}

		Integer heightTemp = 0;
		while (height > (heightTemp = heightTemp + watermark.getHeight())) {
			builder.watermark(getPoint(0, heightTemp), watermark, 1F);

			widthTemp = 0;

			while (width > (widthTemp = widthTemp + watermark.getWidth())) {
				builder.watermark(getPoint(widthTemp, heightTemp), watermark, 1F);
			}
		}
		builder.toFile(new File("xxoo.png"));
		System.out.println(System.currentTimeMillis() - begin);
	}

	public static Position getPoint(int x, int y) {
		return new Position() {

			@Override
			public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft,
					int insetRight, int insetTop, int insetBottom) {
				return new Point(x, y);
			}
		};
	}
}
